using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Reflection;
using System.Resources;


namespace CustomControls
{
    namespace xButtons
    {
        [DefaultProperty("Text")]
        [ToolboxData("<{0}:ConfirmImageButton runat=server></{0}:ConfirmImageButton>")]
        public class ConfirmImageButton : ImageButton
        {
            #region Constant members
            private const string CONFIRMMESSAGE_JS = "CustomControls.ConfirmMessageJS.js";
            #endregion

            [Bindable(true)]
            [Category("ImageButton Confirm Message")]
            [DefaultValue("Are you sure you want to delete the item?")]
            [Localizable(true)]
            [Description("Message to show on the client side")]
            public string MessageText
            {
                get
                {
                    string msgText = "Are you sure you want to delete the item?";

                    object obj = this.ViewState["MessageText"];
                    if (obj != null)
                        msgText = obj.ToString();

                    return msgText;
                }
                set
                {
                    this.ViewState["MessageText"] = value;
                }
            }

            [Bindable(true)]
            [Category("ImageButton Confirm Message")]
            [DefaultValue(true)]
            [Localizable(true)]
            [Description("Enable or not the message confirmation")]
            public bool EnableConfirmMsg
            {
                get
                {
                    bool enableConfirmMsg = true;

                    object obj = this.ViewState["EnableConfirmMsg"];
                    if (obj != null)
                        enableConfirmMsg = (bool)obj;

                    return enableConfirmMsg;
                }
                set
                {
                    this.ViewState["EnableConfirmMsg"] = value;
                }
            }

            protected override void OnInit(EventArgs e)
            {
                // Call base Init.
                base.OnInit(e);

                if (EnableConfirmMsg)
                {
                    if (!string.IsNullOrEmpty(MessageText))
                        this.OnClientClick = "return __ConfirmMessage('" + MessageText + "');";
                }
            }

            protected override void OnLoad(EventArgs e)
            {
                // Call bas Load
                base.OnLoad(e);

                if (EnableConfirmMsg)
                {
                    if (!string.IsNullOrEmpty(MessageText))
                    {
                        // Include the ConfirmMessageJS.js, which includes all Js related functions
                        Type t = this.GetType();
                        string url = Page.ClientScript.GetWebResourceUrl(t, CONFIRMMESSAGE_JS);

                        if (!Page.ClientScript.IsClientScriptIncludeRegistered(t, CONFIRMMESSAGE_JS))
                            Page.ClientScript.RegisterClientScriptInclude(t, CONFIRMMESSAGE_JS, url);
                    }
                }
            }

            protected override void OnPreRender(EventArgs e)
            {
                // Call base PreRender
                base.OnPreRender(e);
            }
        }
    }
}